<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.paperId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="考试标题" prop="paperTitle">
          <el-input v-model="dataForm.paperTitle" placeholder="考试标题"></el-input>
      </el-form-item>
          <el-form-item label="考试时间" prop="examinationTime">
          <el-input v-model="dataForm.examinationTime" placeholder="考试时间"></el-input>
      </el-form-item>
          <el-form-item label="答题时间" prop="answerTime">
          <el-input v-model="dataForm.answerTime" placeholder="答题时间"></el-input>
      </el-form-item>
          <el-form-item label="卷面总分" prop="scores">
          <el-input v-model="dataForm.scores" placeholder="卷面总分"></el-input>
      </el-form-item>
          <el-form-item prop="status" label="题目类型">
        <ren-radio-group v-model="dataForm.status" dict-type="status"></ren-radio-group>
      </el-form-item>
          <el-form-item label="单选题数量" prop="radioNum">
          <el-input v-model="dataForm.radioNum" placeholder="单选题数量"></el-input>
      </el-form-item>
          <el-form-item label="多远题数量" prop="choiceNum">
          <el-input v-model="dataForm.choiceNum" placeholder="多远题数量"></el-input>
      </el-form-item>
          <el-form-item label="是非题数量" prop="nonNum">
          <el-input v-model="dataForm.nonNum" placeholder="是非题数量"></el-input>
      </el-form-item>
          <el-form-item label="单选题分数" prop="fractionRadio">
          <el-input v-model="dataForm.fractionRadio" placeholder="单选题分数"></el-input>
      </el-form-item>
          <el-form-item label="多选题数量" prop="fractionChoice">
          <el-input v-model="dataForm.fractionChoice" placeholder="多选题数量"></el-input>
      </el-form-item>
          <el-form-item label="是非题分数" prop="fractionNon">
          <el-input v-model="dataForm.fractionNon" placeholder="是非题分数"></el-input>
      </el-form-item>
          <el-form-item prop="types" label="考试类别">
        <ren-radio-group v-model="dataForm.types" dict-type="types"></ren-radio-group>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        paperId: '',
        paperTitle: '',
        examinationTime: '',
        answerTime: '',
        scores: '',
        status: '',
        radioNum: '',
        choiceNum: '',
        nonNum: '',
        fractionRadio: '',
        fractionChoice: '',
        fractionNon: '',
        types: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        paperTitle: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        examinationTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        answerTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        scores: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        radioNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        choiceNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nonNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fractionRadio: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fractionChoice: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fractionNon: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        types: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.paperId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/exam/paper/${this.dataForm.paperId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.paperId ? 'post' : 'put']('/exam/paper/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
